﻿/*
 *  Copyright 2012 Cloudbase Solutions Srl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the Microsoft Public License (Ms-PL).  
 */

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Management.Client.Win32;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using Cloudbase.Iis.HostsFileManager.Common;

namespace Cloudbase.Iis.HostsFileManager.Client
{
    public class HostsFileEntryForm : TaskForm
    {
        private ManagementPanel panel1;
        private Label hostLabel;
        private TextBox hostTextBox;
        private Label ipAddressLabel;
        private TextBox ipAddressTextBox;
        private Label commentLabel;
        private TextBox commentTextBox;
        private bool isAdd;
        private bool canAccept = false;
        private string initialHost;

        private IHostsFileModuleService svc;

        public string Host { get { return hostTextBox.Text.Trim();  } }
        public IPAddress IPAddress { get { return IPAddress.Parse(ipAddressTextBox.Text.Trim()); } }
        public string Comment { get { return commentTextBox.Text.Trim(); } }

        protected override bool CanAccept
        {
            get
            {
                return canAccept;
            }
        }

        public HostsFileEntryForm(IServiceProvider serviceProvider, IHostsFileModuleService svc, bool isAdd, string host, IPAddress ipAddress, string comment)
            : base(serviceProvider)
        {
            this.svc = svc;
            this.isAdd = isAdd;
            InitializeComponent();
            SetInitialValues(host, ipAddress, comment);
        }

        private void SetInitialValues(string host, IPAddress ipAddress, string comment)
        {
            if (host != null)
            {
                hostTextBox.Text = host;
                initialHost = host;
            }

            if (ipAddress != null)
                ipAddressTextBox.Text = ipAddress.ToString();

            if (comment != null)
                commentTextBox.Text = comment;
        }
        
        protected override void  OnAccept()
        {
            if (Host != initialHost && svc.ContainsHost(Host))
                ShowMessage(string.Format(StringResource.HostsFileDuplicateMessage, Host), MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            else
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }
        
        private void InitializeComponent()
        {
            panel1 = new ManagementPanel();
            hostLabel = new Label();
            hostTextBox = new TextBox();
            ipAddressLabel = new Label();
            ipAddressTextBox = new TextBox();
            commentLabel = new Label();
            commentTextBox = new TextBox();

            panel1.SuspendLayout();
            panel1.Name = "panel1";
            panel1.Size = new Size(350, 158);
            panel1.Padding = new Padding(1);
            panel1.TabIndex = 0;

            hostLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            hostLabel.Location = new Point(0, 0);
            hostLabel.Name = "hostLabel";
            hostLabel.AutoSize = true;
            hostLabel.TabIndex = 0;
            hostLabel.Text = StringResource.HostLabelCaption;
            hostLabel.TextAlign = ContentAlignment.MiddleLeft;
            panel1.Controls.Add(hostLabel);

            hostTextBox.AutoSize = false;
            hostTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            hostTextBox.Location = new Point(0, 16);
            hostTextBox.Name = "hostTextBox";
            hostTextBox.Size = new Size(250, 20);
            hostTextBox.TabIndex = 1;
            hostTextBox.TextChanged += new EventHandler(hostTextBox_TextChanged);
            panel1.Controls.Add(hostTextBox);

            ipAddressLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            ipAddressLabel.Location = new Point(0, 44);
            ipAddressLabel.Name = "ipAddressLabel";
            ipAddressLabel.AutoSize = true;
            ipAddressLabel.TabIndex = 2;
            ipAddressLabel.Text = StringResource.IPAddressLabelCaption;
            ipAddressLabel.TextAlign = ContentAlignment.MiddleLeft;
            panel1.Controls.Add(ipAddressLabel);

            ipAddressTextBox.AutoSize = false;
            ipAddressTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            ipAddressTextBox.Location = new Point(0, 60);
            ipAddressTextBox.Name = "ipAddressTextBox";
            ipAddressTextBox.Size = new Size(250, 20);
            ipAddressTextBox.TabIndex = 3;
            ipAddressTextBox.TextChanged += new EventHandler(ipAddressTextBox_TextChanged);
            panel1.Controls.Add(ipAddressTextBox);

            commentLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            commentLabel.Location = new Point(0, 88);
            commentLabel.Name = "commentLabel";
            commentLabel.AutoSize = true;
            commentLabel.TabIndex = 4;
            commentLabel.Text = StringResource.CommentLabelCaption;
            commentLabel.TextAlign = ContentAlignment.MiddleLeft;
            panel1.Controls.Add(commentLabel);

            commentTextBox.AutoSize = false;
            commentTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            commentTextBox.Location = new Point(0, 104);
            commentTextBox.Name = "commentTextBox";
            commentTextBox.Size = new Size(250, 20);
            commentTextBox.TabIndex = 5;
            panel1.Controls.Add(commentTextBox);

            panel1.ResumeLayout();

            SetContent(panel1);

            if(isAdd)
                Text = StringResource.HostsFileEntryFormTitleAdd;
            else
                Text = StringResource.HostsFileEntryFormTitleEdit;

            ClientSize = new Size(320, 240);

            UpdateTaskForm();
        }

        void ipAddressTextBox_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        void hostTextBox_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private void CheckInput()
        {
            bool oldCanAccept = canAccept;

            canAccept = (hostTextBox.Text.Trim().Length > 0 &&
                         ipAddressTextBox.Text.Trim().Length > 0 && ValidateIPAddress());

            if (canAccept != oldCanAccept)
                UpdateTaskForm();
        }

        private bool ValidateIPAddress()
        {
            IPAddress ipAddr;
            return IPAddress.TryParse(ipAddressTextBox.Text, out ipAddr);
        }
    }
}
